#include "headfile.h"

short int z_zero_drift = 0;//ȥ��Ư��
unsigned int knob_count=0;//��ť����
unsigned char bi_flag=0;
void www_delayms(uint16 ms)
{
	for(;ms>0;ms--)
	{
			unsigned char i, j;

			i = 30;
			j = 43;
			do
			{
				while (--j);
			} while (--i);
	}
}

void mpu_zero_drift_compensation(void) //���Ư�Ʋ����������Ⱥ󣬰�����������Ҫ��������ɼ���������������
{
	unsigned char i=0;
	int z_total=0;
	for(i=0;i<50;i++)
	{
		mpu6050_get_gyro();
		z_total+=mpu_gyro_z;
		delay_ms(10);
	}
	z_zero_drift = z_total/50;
//	SHOW_CLEAR(IPS114_BGCOLOR);
	oled_fill(0x00);
	
	
//	unsigned char i, Count = 50;
//    int temp=0;

//    for(i=0;i<Count;i++)
//      {
//          mpu6050_get_gyro();
//          delay_ms(2);
//          temp+=mpu_gyro_z;
//      }
//    z_zero_drift=(temp/Count);
//	oled_fill(0x00);
}

void Bi(void)
{
	pwm_freq(PWMA_CH1N_P11, YSQ_FRE, 5000);
	bi_flag=1;
}

unsigned char get_knob(void)
{
 unsigned char i=0;
 if(knob_count!=0){i=knob_count;knob_count = 0;}
 return i;
}

unsigned char Key_Down(PIN_enum pin)
{
	//FiveK_Left   
  if(pin == FiveK_Left)
	{
		if(!FiveK_Left)
    {
       www_delayms(15);
       if(!FiveK_Left)
       {
           while(!FiveK_Left);
           return 1;
       }
    }
    return 0;
	}
	//FiveK_Right  
	if(pin == FiveK_Right)
	{
		if(!FiveK_Right)
    {
       www_delayms(15);
       if(!FiveK_Right)
       {
           while(!FiveK_Right);
           return 1;
       }
    }
    return 0;
	}
	//FiveK_Down   
	if(pin == FiveK_Down)
	{
		if(!FiveK_Down)
    {
       www_delayms(15);
       if(!FiveK_Down)
       {
           while(!FiveK_Down);
           return 1;
       }
    }
    return 0;
	}
	//FiveK_Up     
	if(pin == FiveK_Up)
	{
		if(!FiveK_Up)
    {
       www_delayms(15);
       if(!FiveK_Up)
       {
           while(!FiveK_Up);
           return 1;
       }
    }
    return 0;
	}
	//FiveK_Mid    
	if(pin == FiveK_Mid)
	{
		if(!FiveK_Mid)
    {
       www_delayms(15);
       if(!FiveK_Mid)
       {
           while(!FiveK_Mid);
           return 1;
       }
    }
    return 0;
	}
	//Key1         
	if(pin == Key1)
	{
		if(!Key1)
    {
       www_delayms(15);
       if(!Key1)
       {
           while(!Key1);
           return 1;
       }
    }
    return 0;
	}
	return 0;
}

unsigned char Key_Down_Continue(PIN_enum pin)
{
	//FiveK_Left   
  if(pin == FiveK_Left)
	{
		if(!FiveK_Left)
    {
       www_delayms(150);
       if(!FiveK_Left)
       {
           return 1;
       }
    }
    return 0;
	}
	//FiveK_Right  
	if(pin == FiveK_Right)
	{
		if(!FiveK_Right)
    {
       www_delayms(150);
       if(!FiveK_Right)
       {
           return 1;
       }
    }
    return 0;
	}
	//FiveK_Down   
	if(pin == FiveK_Down)
	{
		if(!FiveK_Down)
    {
       www_delayms(150);
       if(!FiveK_Down)
       {
           return 1;
       }
    }
    return 0;
	}
	//FiveK_Up     
	if(pin == FiveK_Up)
	{
		if(!FiveK_Up)
    {
       www_delayms(150);
       if(!FiveK_Up)
       {
           return 1;
       }
    }
    return 0;
	}
	//FiveK_Mid    
	if(pin == FiveK_Mid)
	{
		if(!FiveK_Mid)
    {
       www_delayms(150);
       if(!FiveK_Mid)
       {
           return 1;
       }
    }
    return 0;
	}
	//Key1
	if(pin == Key1)
	{
		if(!Key1)
    {
       www_delayms(150);
       if(!Key1)
       {
           return 1;
       }
    }
    return 0;
	}
	return 0;
}

float get_Battery(void)
{
    unsigned int elec_quantity = adc_once(ADC_P12, ADC_8BIT);
    return 5*(float)elec_quantity/256.0;
}

void int_Limit(int* limit_object,int limit_max,int limit_min)
{
	*limit_object = *limit_object > limit_max?limit_max:*limit_object<limit_min?limit_min:*limit_object;
}

void Board_Common_Init(void)
{
	//����led�������ſ����ó�ʼ��
	gpio_mode(LED_R, GPO_PP);
	gpio_mode(LED_G,GPO_PP);
	gpio_mode(LED_B, GPO_PP);
	
	gpio_mode(FiveK_Left , GPI_IMPEDANCE);gpio_pull_set(FiveK_Left,PULLUP);
	gpio_mode(FiveK_Right, GPI_IMPEDANCE);gpio_pull_set(FiveK_Right,PULLUP);
	gpio_mode(FiveK_Mid  , GPI_IMPEDANCE);gpio_pull_set(FiveK_Mid,PULLUP);
	gpio_mode(FiveK_Up   , GPI_IMPEDANCE);gpio_pull_set(FiveK_Up,PULLUP);
	gpio_mode(FiveK_Down , GPI_IMPEDANCE);gpio_pull_set(FiveK_Down,PULLUP);
	gpio_mode(Key1       , GPI_IMPEDANCE);gpio_pull_set(Key1,PULLUP);
	gpio_mode(XN_SW       , GPI_IMPEDANCE);gpio_pull_set(Key1,PULLUP);//��ť
	
	//simiic_init();
	
	
//	AT_Init(AT_load,1);
	
	exit_init(INT1_P33,FALLING_EDGE);//��ť
	
	//�ȶ��������Ľſڵ�ƽ
	pwm_init(PWMA_CH1N_P11, YSQ_FRE, 0); //�˴���������Ƶ���������ã�����ĵ����ʼ���Ḳ��
	
	//UART�������ó�ʼ��
	uart_init(UART_3, UART3_RX_P50, UART3_TX_P51, 9600, TIM_2);//(hc-05)
	uart_init(UART_1, UART1_RX_P36, UART1_TX_P37, 9600, TIM_2);//(V831)
	//��ʱ���ж����ó�ʼ��	
	pit_timer_ms(TIM_0, 10);		//10ms�ɼ�������һ�Σ�6050��������100hz�Ĳ���Ƶ��

	//lcd��Ļ��ʼ��(�������ȷ����ʼ���Ƿ�������)
	SHOW_INIT();
	
	//����ſ�
	Steer_Init();
	
	//flash����ѡ��
	delay_ms(10);
//canshu_select(AT_read);
	AT_Init(AT_read,1);

}



		//�����ǳ�ʼ��
//		mpu6050_init();
//		G(1);
//		mpu_zero_drift_compensation();
		//delay_ms(10);
//		//��������ʼ��
//		Encoder_Init();
//		Motor_Init();//�ұߵ����j8 
		//��λ����ʼ��
		//Upper_Computer_Init();
		//���Ư�Ʋ����������Ⱥ󣬰�����������Ҫ��������ɼ���������������
//		SHOW(0,3,"dont move!");


